package com.base.core.context.utils.dingtalk;

import com.base.core.context.utils.dingtalk.bo.OapiRobotSendBO;
import com.gitee.magic.context.ConverterEditorUtils;
import com.gitee.magic.core.exception.ApplicationException;
import com.gitee.magic.core.json.JsonObject;
import com.gitee.magic.framework.base.rest.HttpRequest;
import com.gitee.magic.framework.base.rest.HttpWrapper;

/**
 * @see https://open-dev.dingtalk.com/apiExplorer#/?devType=org&api=dingtalk.oapi.robot.send
 * @author start
 *
 */
public class DingTalkRobotService {

	private String url="https://oapi.dingtalk.com";
	
	public void send(String accessToken,OapiRobotSendBO bo) {
		HttpRequest request = new HttpRequest(this.url+"/robot/send");
		request.setHeader(HttpWrapper.CONTENTTYPE, HttpWrapper.CONTENTTYPE_JSON);
		request.setUrlParam("access_token", accessToken);
		request.setBodyContent(ConverterEditorUtils.converterObject(bo).toString());
		HttpWrapper wrapper = new HttpWrapper();
		String content=wrapper.start(request);
		JsonObject json = new JsonObject(content);
		if (json.getInt("errcode") != 0) {
			throw new ApplicationException(content);
		}
	}

	public static void main(String[] args) throws Exception {
		DingTalkRobotService dt = new DingTalkRobotService();
		String accessToken="268ff5d566f3be3f9117b18d773170ce18830c1beb707863f15fba8963057075";
		OapiRobotSendBO bo=new OapiRobotSendBO();
		bo.setMsgtype("text");
		OapiRobotSendBO.Text text=new OapiRobotSendBO.Text();
		text.setContent("【Google】这是测2试111");
		bo.setText(text);
		OapiRobotSendBO.At at=new OapiRobotSendBO.At();
		at.setAtAll(true);
		bo.setAt(at);
		dt.send(accessToken,bo);
	}

}
